package com.guigu.youxuan.controller;

import com.github.pagehelper.PageInfo;
import com.guigu.youxuan.pojo.Gather;
import com.guigu.youxuan.pojo.GoodsInfo;
import com.guigu.youxuan.pojo.Warehouse;
import com.guigu.youxuan.pojo.dto.GatherDto;
import com.guigu.youxuan.service.IGatherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import com.guigu.youxuan.pojo.Gather;
import com.guigu.youxuan.pojo.PurchaseOrderDetails;
import com.guigu.youxuan.pojo.dto.GatherMakeDto;
import com.guigu.youxuan.pojo.dto.PurchaseAndDetailDto;
import com.guigu.youxuan.pojo.dto.SupplierGoodsDto;
import com.guigu.youxuan.service.IGatherService;
import com.guigu.youxuan.utils.NumberUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author liweixin
 * @since 2024-05-15
 */
@CrossOrigin
@Controller
@RequestMapping("/gather")
public class GatherController {
    @Autowired
    private IGatherService iGatherService;
    @Autowired
    private NumberUtils numberUtils;

    /**
     * 入库审核查询状态为S001-1待审核的信息
     * @param currentPage
     * @param pageSize
     * @return
     */
    @RequestMapping("/InStoragePage/{currentPage}/{pageSize}")
    @ResponseBody
    public PageInfo<Gather> InStorageFuhePage(
                                              @PathVariable int currentPage,
                                              @PathVariable int pageSize){
        PageInfo<Gather> page=this.iGatherService.InStorageFuhePage(currentPage,pageSize);
        return page;
    }

    /**
     * 入库列表查询所有入库信息
     * @param currentPage
     * @param pageSize
     * @return
     */
    @RequestMapping("/InStorageListPage/{currentPage}/{pageSize}")
    @ResponseBody
    public PageInfo<Gather> InStorageListPage(
                                              @PathVariable int currentPage,
                                              @PathVariable int pageSize){
        PageInfo<Gather> page=this.iGatherService.InStorageListPage(currentPage,pageSize);
        return page;
    }
    /**
     * 根据入库表id查询对应的入库信息
     * @param id
     * @return
     */
    @RequestMapping("/InStorageFuheListByid/{id}")
    @ResponseBody
    public Gather InStorageFuheListByid(@PathVariable int id){
        Gather warehouse=this.iGatherService.getById(id);
        return warehouse;
    }
    /**
     * 入库审核信息复核未通过
     * @param gather
     * @return
     */
    @RequestMapping("/NoFuheReviewList")
    @ResponseBody
    public String NoFuheReviewList(@RequestBody Gather gather) {
        this.iGatherService.updateById(gather);
        gather.setCheckTag("S001-2");
        System.out.println("审核"+gather.getCheckTag()+"审核");
        System.out.println("id"+gather.getWarehouseId()+"id");
        boolean flag=this.iGatherService.updateById(gather);
        if(flag){
            return "ok";
        }
        return "error";
    }

    /**
     * 入库审核通过
     * @param gatherDto
     */
    @RequestMapping("/YesFuheReviewList")
    @ResponseBody
    public String YesFuheReviewList(@RequestBody GatherDto gatherDto) {
        iGatherService.YesFuheReviewList(gatherDto);
        return "入库审核成功";
    }

    /**
     * 生成采购入库单
     * @param dto
     * @return
     */
    @RequestMapping("/gatherMake")
    @ResponseBody
    public String gatherMake(@RequestBody GatherMakeDto dto) {
        int flag = 0;
        List<PurchaseOrderDetails> list = dto.getPurchaseOrderDetails();
        Map<Integer,List<PurchaseOrderDetails>> map = new HashMap();
        for(PurchaseOrderDetails p : list) {
            Integer warehouseId = p.getWarehouseId();
            // 如果map中还没有这个warehouseId的列表，则创建一个新的列表
            map.putIfAbsent(warehouseId, new ArrayList<>());
            // 将p添加到对应的列表中
            map.get(warehouseId).add(p);
        }
        for (Map.Entry<Integer, List<PurchaseOrderDetails>> entry : map.entrySet()) {
            // 获取键（warehouseId）
            Integer warehouseId = entry.getKey();

            // 获取与键关联的PurchaseOrderDetails对象列表
            List<PurchaseOrderDetails> purchaseOrderDetailsList = entry.getValue();

            Gather gather = new Gather();
            String gatherOrderNumber = numberUtils.getTypeNumber("200");
            gather.setGatherOrderNumber(gatherOrderNumber);
            gather.setRegister(dto.getRegister());
            gather.setReasonexact(dto.getReasonexact());
            gather.setAmountSum(0);
            gather.setCostPriceSum(0);
            gather.setPurchaseId(dto.getPurchaseOrder().getPurchaseId());

            // 遍历列表中的每个PurchaseOrderDetails对象
            for (PurchaseOrderDetails purchaseOrderDetails : purchaseOrderDetailsList) {
                gather.setAmountSum(purchaseOrderDetails.getCount()+gather.getAmountSum());
                gather.setCostPriceSum(purchaseOrderDetails.getPrice()*purchaseOrderDetails.getCount()+gather.getCostPriceSum());
            }

            Integer supplierId = dto.getPurchaseOrder().getSupplierId();

            iGatherService.gatherMake(gather,purchaseOrderDetailsList,supplierId);
            flag++;
        }


        return "已生成"+flag+"条采购入库单，请前往审核！";
    }
}
